<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/27 0027
 * Time: 11:05
 */

namespace App\Helpers;

use Illuminate\Support\Facades\Log;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
use Illuminate\Support\Facades\Redis;

class Tools
{
    /**
     * 写入成功返回
     * @param string $message
     */
    public static function success($message = '写入成功', $data = array())
    {
        if(!$data) {
            $response = [
                'status' => 'success',
                'status_code' => 200,
                'error' => 0,
                'message' => $message
            ];
        } else {
            $response = [
                'status' => 'success',
                'status_code' => 200,
                'error' => 0,
                'message' => $message,
                'data' => $data
            ];
        }

        return $response;
    }

    /**
     * 写入成功返回
     * @param string $message
     */
    public static function setData($data = [], $error = 0, $message = '写入失败'){
        if(!$error) {
            $response = [
                'status' => 'success',
                'status_code' => 200,
                'error' => 0,
                'data' => $data
            ];
        } else {
            $response = [
                'status' => 'failed',
                'status_code' => 200,
                'error' => 1,
                'message' => $message,
                'data' => $data
            ];
        }

        return $response;
    }

    /**
     * 写入失败返回
     * @param string $message
     */
    public static function error($message = '写入失败')
    {
        $response = [
            'status' => 'failed',
            'status_code' => 500,
            'error' => 1,
            'message' => $message
        ];
        return $response;
    }



    /**
     * 单个日志输出
     * @param string $content
     */
    public static function logInfo($content)
    {
        if (env('LOG_ON', true)) {
            Log::info('==========================');
            Log::info($content);
            Log::info('==========================');
        }
    }

    /**
     * 单个错误日志输出
     * @param string $content
     */
    public static function logError($content)
    {
        Log::error('**************************');
        Log::error($content);
        Log::error('**************************');
    }

    /**
     * 多个日志一次性输出
     * @param string $subject
     * @param string $content
     */
    static public function singleLog($content, $subject = null)
    {
        if (!isset($GLOBALS['debugArray'])) {
            $GLOBALS['debugArray'] = array();
        }

        if ($subject) {
            array_push($GLOBALS['debugArray'], $subject);
            array_push($GLOBALS['debugArray'], '==========================');
        }

        if ($content) {
            array_push($GLOBALS['debugArray'], $content);
            array_push($GLOBALS['debugArray'], '--------------------------');
        }

        return true;
    }

    /**
     * 生成10位随机码，与二维码
     * @param  int    $id
     * @param  string $url  http://baidu.com/
     * @param  string $path
     */
    public static function createCode($id, $url, $path = 'qrcodes')
    {
        // 生成券码
        $code = self::orderCodeCheck();
        $urlData = json_encode([
            'code' => $code,
            'merchant_id' => $id
        ]);

        // 创建文件夹保存图片
        if (!file_exists(public_path("/$path"))) {
            mkdir(public_path("/$path"));
        }

        $qrcodeUrl = "$path/$code.svg";

        // 生成二维码
        QrCode::format('svg')->size(200)->generate($urlData, $qrcodeUrl);

        return [
            'code' => $code,
            'qrcode_url' => $qrcodeUrl
        ];
    }

    /**
     * 生成唯一券码
     * @param  int    $id
     */
    protected static function orderCodeCheck()
    {
        $code = rand((int)10, (int)99) . date("m") . rand((int)10, (int)99) . date("d") . rand((int)10, (int)99);

        $k = "order:number:" . date("MD");

        if (!Redis::sadd($k, $code)) {
            self::orderCodeCheck();
        } else {
            return $code;
        }
    }

    /**
     * @name   小数位截取格式化金额  例如 100000.00 -> 100,000.00
     * @param  float $num [格式化前的金额]
     * @param  integer $dist [保留的小数位数]
     * @param  BOOL $zeroComplete [小数位不够dist时，是否用0补齐]
     * @return [type]        [description]
     */
    public static function numberFormat($num = 0, $dist = 2, $zeroComplete = TRUE)
    {
        if (!preg_match('/^(-?\d+)(\.\d+)?$/', $num)) {
            return $num;
        }
        if ($dist > 4) {
            $dist = 4;
        } else if ($dist <= 0) {
            $dist = 0;
        }
        if (!is_bool($zeroComplete)) {
            $zeroComplete = TRUE;
        }
        $newNum = floor($num * pow(10, $dist)) / pow(10, $dist);
        if (!$zeroComplete) {
            //去掉小数末尾的0
            $newNum = floatZeroCut($newNum);
            $pos = strpos(strval($newNum), '.');//获取小数点位置
            if (!$pos) {
                //如果没找到
                $dist = 0;
            } else {
                $dist = strlen(strval($newNum)) - $pos - 1;
            }
        }
        $result = number_format($newNum, $dist);
        return $result;
    }

    public static function p($data)
    {
        echo "<pre>";
        print_r($data);
        echo "</pre>";
        exit;
    }
}